package com.example.service.impl;

import com.example.pojo.CodeTale;
import com.example.service.MemcachedCodeService;
import com.example.utils.CodeUtils;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeoutException;
@Service
public class MemcachedCodeServiceImpl implements MemcachedCodeService {


    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private MemcachedClient memcachedClient;
    @Override
    public String sendUserCode(String tela) throws InterruptedException, TimeoutException, MemcachedException {
        String code = codeUtils.getCode(tela);
        memcachedClient.set(tela,10,code);
        return code;
    }

    @Override
    public boolean checkCode(CodeTale codeTale) throws InterruptedException, TimeoutException, MemcachedException {
        Object o = memcachedClient.get(codeTale.getTale());
        String string = o.toString();
        return codeTale.getCode().equals(string);
    }
}
